class UserInfo < ActiveRecord::Base
  has_one :user, :dependent => :destroy
  has_one :company, :dependent => :destroy

  has_many :orders
  
  before_validation :clone_shipping_address, :remove_company

  after_initialize :default_attributes
  
  belongs_to :billing_address,  :class_name => "Address", :foreign_key => :billing_address_id, :dependent => :destroy
  belongs_to :shipping_address, :class_name => "Address", :foreign_key => :shipping_address_id, :dependent => :destroy

  accepts_nested_attributes_for :company, :allow_destroy => true, :reject_if => proc {|attr| attr['name'].blank? && attr['ico'].blank?}
  accepts_nested_attributes_for :billing_address, :allow_destroy => true, :reject_if => proc {|attr| attr['city'].blank? && attr['street'].blank? && attr['zip'].blank?}
  accepts_nested_attributes_for :shipping_address, :allow_destroy => true
  accepts_nested_attributes_for :user

  validates :name, :length => {:maximum => 15}, :presence => true
  validates :surname, :length => {:maximum => 30}, :presence => true
  validates :phone, :format => {:with => Format::PHONE_REGEX}, :presence => true

  attr_accessor :use_billing_address, :ignore_company

  delegate :email, :discount, :to => :user

  def default_attributes
    if self.new_record?
      self.use_billing_address = '1'
    else
      self.use_billing_address = '1' if self.shipping_address.same_as(self.billing_address)
    end

    if self.new_record?
      self.ignore_company = '1'
    else
      self.ignore_company = '1' if self.company.new_record?
    end
  end

  alias new_company company
  def company
    new_company || build_company
  end

  alias new_shipping_address shipping_address
  def shipping_address
    new_shipping_address || build_shipping_address
  end

  alias new_billing_address billing_address
  def billing_address
    new_billing_address || build_billing_address
  end

  alias new_user user
  def user
   new_user || build_user
  end

  def clone_shipping_address
    if self.use_billing_address == '1'
      if self.billing_address.nil?
        self.billing_address = self.shipping_address.clone_address
      else
        self.billing_address.attributes = self.shipping_address.attributes
      end
    end
  end

  def has_no_role
    include(:user => :roles).where("user_roles.role IS NULL")
  end

  def has_role
    joins(:user => :roles)
  end

  def self.has_orders?
    orders
  end

  def same_as?(other_user)
    attributes.except("id", "user_id", "company_id", "note", "updated_at", "created_at") == other_user.attributes.except("id", "user_id", "company_id", "note", "updated_at", "created_at")
  end
  alias same_as same_as?

  def clone_user_info
    UserInfo.new(self.attributes.except("id", "updated_at", "created_at"))
  end

  def whole_name
    self.name + " " + self.surname
  end

  def remove_company
    if self.ignore_company == '1'
      if self.company.new_record?
        self.company.delete
      else
        self.company.destroy
      end
    end
  end
end
